import { userModel } from '@/models'

export const addFavFn = (customerId: any, gameItem: any) => {
  const maxFavNum = 12
  let favListString: any
  let favList: any[] = []

  if (Object.hasOwnProperty.call(localStorage, customerId + '_favList')) {
    favListString = localStorage.getItem(customerId + '_favList')
    favList = JSON.parse(favListString)
  }
  if (favList.length > 0) {
    let inist = false
    favList.map((item: any, index: number) => {
      if (item.gameId == gameItem.gameId) {
        favList.splice(index, 1)
        inist = true
      }
    })
    if (!inist) {
      if (favList.length >= maxFavNum) {
        favList.splice(0, 1) // 删除最早的一个
        favList.push(gameItem)
      } else {
        favList.push(gameItem)
      }
    }
  } else {
    favList.push(gameItem)
  }
  localStorage.setItem(customerId + '_favList', JSON.stringify(favList))
}
export const checkFav = (customerId: any, gameItem: any, cb: any) => {
  let favListString: any
  let favList = []
  if (Object.hasOwnProperty.call(localStorage, customerId + '_favList')) {
    favListString = localStorage.getItem(customerId + '_favList')
    favList = JSON.parse(favListString)
  }
  if (favList.length > 0) {
    let inist = false
    favList.map((item: any) => {
      if (item.gameId == gameItem.gameId) {
        cb(true)
        inist = true
      }
    })
    !inist && cb(false)
  } else {
    cb(false)
  }
}
//格式化 返回时间戳
export const getDateString = (dayCount: number) => {
  const nowDate = new Date()
  const toDayZeroClock = new Date(nowDate.toLocaleDateString())
  const dateCount = 24 * 60 * 60 * 1000
  const dateString = toDayZeroClock.getTime() - dayCount * dateCount
  // console.log(new Date(dateString))
  return dateString
}
export const timeLeft = (timeLeft: any, yms?: any) => {
  let d = 0,
    h = 0,
    m = 0,
    s = 0
  if (timeLeft >= 0) {
    d = Math.floor(timeLeft / 3600 / 24)
    h = Math.floor((timeLeft / 60 / 60) % 24)
    m = Math.floor((timeLeft / 60) % 60)
    s = Math.floor(timeLeft % 60)
  }
  const str = yms
    ? d + ' day ' + h + ' : ' + m + ' : ' + s + ''
    : d + ' day ' + h + ' hour ' + m + ' min ' + s + ' s'
  return str
}
export const timeFormatStr = (timesecond: number) => {
  if (!timesecond || timesecond <= 0) {
    return '00:00:00'
  }
  const d = Math.floor(timesecond / 3600 / 24)
  const h = Math.floor((timesecond / 60 / 60) % 24)
  const m = Math.floor((timesecond / 60) % 60)
  const s = Math.floor(timesecond % 60)

  const dd = d > 0 ? d + ' day ' : ''
  const hh = h <= 9 ? '0' + h : h
  const mm = m <= 9 ? '0' + m : m
  const ss = s <= 9 ? '0' + s : s

  return dd + hh + ':' + mm + ':' + ss
}
// 格式化返回时间 格式
export const getNowFormatDate = (timestamp: any) => {
  const isSecond = true
  if (isSecond) {
    timestamp = timestamp * 1000
  }

  const date = new Date(timestamp)
  const year = date.getFullYear()
  const month = (date.getMonth() + 1).toString().padStart(2, '0')
  const day = date.getDate().toString().padStart(2, '0')
  const hour = date.getHours().toString().padStart(2, '0')
  const minute = date.getMinutes().toString().padStart(2, '0')
  const second = date.getSeconds().toString().padStart(2, '0')
  const formattedDate = `${year}-${month}-${day} ${hour}:${minute}:${second}`
  return formattedDate
}
export const uaIs = () => {
  const u = navigator.userAgent
  return {
    trident: u.indexOf('Trident') > -1, //IE内核
    presto: u.indexOf('Presto') > -1, //opera内核
    webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
    gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
    mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
    ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
    android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器
    iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
    iPad: u.indexOf('iPad') > -1, //是否iPad
    webApp: u.indexOf('Safari') == -1, //是否web应该程序，没有头部与底部
  }
}
export const windowOpenLink = (
  url: string,
  linkMethod?: any,
  navigate?: any,
  newWindow?: boolean,
) => {
  if (url) {
    if (!linkMethod || linkMethod == 2) {
      if (newWindow) {
        window.open(url)
        return
      }
      if (uaIs().ios) {
        window.location.href = url
      } else {
        window.open(url)
      }
    } else if (linkMethod == 1) {
      navigate && navigate(url)
    }
  }
}
export const deepEqual = (obj1: any, obj2?: any) => {
  if (typeof obj1 !== 'object' || typeof obj2 !== 'object') {
    return obj1 === obj2
  }

  const keys1 = Object.keys(obj1)
  const keys2 = Object.keys(obj2)

  if (keys1.length !== keys2.length) {
    return false
  }

  for (const key of keys1) {
    if (!keys2.includes(key) || !deepEqual(obj1[key], obj2[key])) {
      return false
    }
  }

  return true
}
